package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.enjoyor.soa.traffic.core.efoms.domain.CheckRefreshInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.service.ICheckRefreshInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IDevStatusInfoService;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

@Async
@Order(1)
@Component
public class DevStatusRefreshThread implements CommandLineRunner{
	
	//读取配置文件中的设备巡检周期；
	@Value("${xhj.Refresh.Cycle}")
	private int xhjCycle;
	@Value("${dj.Refresh.Cycle}")
	private int djCycle;
	@Value("${sp.Refresh.Cycle}")
	private int spCycle;
	@Value("${yd.Refresh.Cycle}")
	private int ydCycle;
	@Value("${dc.Refresh.Cycle}")
	private int dcCycle;
	@Value("${kk.Refresh.Cycle}")
	private int kkCycle;
	@Value("${wb.Refresh.Cycle}")
	private int wbCycle;
	
	//读取配置文件中的设备类型；
	@Value("${xhj.Dev.Type}")
	private String xhjDevType;
	@Value("${dj.Dev.Type}")
	private String djDevType;
	@Value("${sp.Dev.Type}")
	private String spDevType;
	@Value("${yd.Dev.Type}")
	private String ydDevType;
	@Value("${dc.Dev.Type}")
	private String dcDevType;
	@Value("${kk.Dev.Type}")
	private String kkDevType;
	@Value("${wb.Dev.Type}")
	private String wbDevType;
	
	//读取配置文件中的刷新设定时间；
	@Value("${cycle.start.date}")
	private String cycleStaDate;
	
	@Autowired
	private ICheckRefreshInfoService checkRefreshInfoService;
	
	@Autowired
	private IDevStatusInfoService devStatusInfoService;
	
	@Override
	public void run(String... args) throws Exception {
		String date = TimeHelper.dateToString(new Date(),"yyyy-MM-dd");//获取当前系统时间；
		while (true) {	
			if(xhjDevType.equals("REPDEVTYPE01")){
				this.dealCheckRefresh(xhjDevType,cycleStaDate,xhjCycle);
			}if (djDevType.equals("REPDEVTYPE04")) {
				this.dealCheckRefresh(djDevType,cycleStaDate,djCycle);
			}if (spDevType.equals("REPDEVTYPE02")) {
				this.dealCheckRefresh(spDevType,cycleStaDate,spCycle);
			}if (ydDevType.equals("REPDEVTYPE08")) {
				this.dealCheckRefresh(ydDevType,cycleStaDate,ydCycle);
			}if (dcDevType.equals("REPDEVTYPE05")) {
				this.dealCheckRefresh(dcDevType,cycleStaDate,dcCycle);
			}if (kkDevType.equals("REPDEVTYPE03")) {
				this.dealCheckRefresh(kkDevType,cycleStaDate,kkCycle);
			}if (wbDevType.equals("REPDEVTYPE07")) {
				this.dealCheckRefresh(wbDevType,cycleStaDate,wbCycle);
			}
			try {
				Thread.sleep(2000*60*60);//两个小时校验一次；
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	}
	
	//数据校验；
	private void dealCheckRefresh(String devTypeCode,String startDate,int cycle)throws Exception{
		String date = TimeHelper.dateToString(new Date(),"yyyy-MM-dd");
		CheckRefreshInfo checkRefreshInfo = new CheckRefreshInfo();
		checkRefreshInfo = checkRefreshInfoService.getCheckRefreshInfo(devTypeCode);
		//巡检设备还没有被刷新过；（系统第一次启动，）
		if(checkRefreshInfo!=null){
			//获取当前列表巡检刷新时间；
			String refreshDate = checkRefreshInfo.getRefreshDate();
			//计算余数；
			int differDay = TimeHelper.getDifferDay(refreshDate, date)%cycle;
			//计算最近需要刷新的日期是多少；
			String refreshDay = TimeHelper.addDate(date, -differDay);
			if(!refreshDate.equals(refreshDay)){
				DeviceStatusInfo refreshDeviceStatusInfo = new DeviceStatusInfo();
				refreshDeviceStatusInfo.setDevTypeCode(devTypeCode);
				devStatusInfoService.refreshCheckResult(refreshDeviceStatusInfo);
				checkRefreshInfo.setRefreshDate(refreshDay);
				checkRefreshInfoService.updateCheckRefreshInfo(checkRefreshInfo);
				System.out.println("刷新=======================");
			}
		}else{
			//计算系统时间和自定义时间中间想差多少天
			int differDay = TimeHelper.getDifferDay(startDate, date);
			//如果系统时间大于或等于自定义开始时间（若自定义开始时间大于系统时间，则不做处理；）
			if(differDay>=0){
				DeviceStatusInfo refreshDeviceStatusInfo = new DeviceStatusInfo();
				CheckRefreshInfo newCheckRefreshInfo = new CheckRefreshInfo();
				int day = TimeHelper.getDifferDay(startDate, date)%cycle;
				String resFreshDate = TimeHelper.addDate(date, -day);
				newCheckRefreshInfo.setDevTypeCode(devTypeCode);
				newCheckRefreshInfo.setRefreshDate(resFreshDate);
				devStatusInfoService.refreshCheckResult(refreshDeviceStatusInfo);
				checkRefreshInfoService.insertCheckRefreshInfo(newCheckRefreshInfo);
				System.out.println("刷新=====================+记录生成=============");
			}
		}
	}
	
}
